package com.bdcom.autotest.ui.step;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bdcom.autotest.MainFrame;
import com.bdcom.autotest.interaction.step.StepBean;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.ui.CenterPanel;
import com.bdcom.autotest.ui.gif.Gif;
import com.bdcom.autotest.ui.gif.ImgFactory;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;

public class StepItem extends JPanel implements ActionListener{
	
	private static Logger log = LoggerFactory.getLogger(StepItem.class);

	private StepBean bean;
	
	private int index;
	private JPanel descPanel ;
	private JButton operBtn ;
	
	private JLabel imageLabel = null;
	private int imagehh = 0;
	private int imageww = 0;
	
	public StepItem(StepBean bean , int index ) {
		
		this.bean = bean;
		this.index = index;
		bean.setItem(this);
		
		UIUtil.setCompBack(this);
		
		initUI();
	}


	private JLabel tipLabel ;
	private void initUI() {
		this.setLayout(new FlowLayout(  FlowLayout.LEFT ) );
		descPanel = new JPanel(  new BorderLayout() );
		UIUtil.setCompBack(descPanel);
		
		String type = bean.getType();
		if(  bean.type_tip.equals(type)  ) {
			JTextArea areaDesc = new JTextArea();
			areaDesc.setLineWrap(true);
			areaDesc.setEditable(false);
			areaDesc.setText(  "注意:" + bean.getDesc());
			areaDesc.setPreferredSize(new Dimension(470 , bean.getHh() - 6 ) );
			descPanel.add(areaDesc);
			this.add(descPanel);
			return;
		}
		
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		descPanel.setBorder(border);
		JTextArea areaDesc = new JTextArea();
		areaDesc.setLineWrap(true);
		areaDesc.setEditable(false);
		areaDesc.setText( ( index+1)+"." + bean.getDesc());
		areaDesc.setPreferredSize(new Dimension(470 , bean.getHh() ) );
		descPanel.add(areaDesc);
		
		
		//初始化提示图片
//		String imgPath = bean.getParent().getPicDir()+"/" + (1+index) + Prop.JPG;
//		File ff = new File(imgPath);
//		if(  ff.exists()  ) {
//			try {
//				double maxw = 470;
//				double maxh = 400;
//				BufferedImage read = ImageIO.read( ff );
//				int width = read.getWidth();
//				int height = read.getHeight();
//				imageww = width;
//				imagehh = height;
//				if(  width >= maxw || height>maxh ) {
//					double a = maxw/width;
//					double b = maxh/ height;
//					
//					double usescale =a; 
//					if(  a>b  ) {
//						usescale = b; 
//					}
//					imageww = (int) (usescale*width);
//					imagehh = (int) (usescale * height);
//				}
//				Image loadIconScale = ImgFactory.loadIconScale( ff  , imageww , imagehh ) ;
//				ImageIcon imageIcon = new ImageIcon( loadIconScale );
//				imageLabel = new JLabel("'") ;
//				imageLabel.setIcon(imageIcon);
//				imageLabel.setPreferredSize(new Dimension( imageww , imagehh) );
//				UIUtil.setCompBack(imageLabel);
//				addClickListener(imageLabel);
//			} catch ( Exception e) {
//				log.error( "" ,e );
//			}
//		}
		
		String imgKey = bean.getParent().getPicDir()+"/" + (1+index) ;
		Image image = Prop.keyToImage.get(imgKey);
		if(  image!=null ) {
			imageww = image.getWidth(null);
			imagehh =image.getHeight(null);
			ImageIcon imageIcon = new ImageIcon( image );
			imageLabel = new JLabel("'") ;
			imageLabel.setIcon(imageIcon);
			imageLabel.setPreferredSize(new Dimension( imageww , imagehh) );
			UIUtil.setCompBack(imageLabel);
			addClickListener(imageLabel);
		}
		
		
		int descPanelh = bean.getHh() + 17 ;
		if(  index ==0 && imageLabel!=null  ) {
			descPanelh = descPanelh + imagehh;
			descPanel.add(imageLabel , BorderLayout.SOUTH );
		}
		descPanel.setPreferredSize(new Dimension( 490 , descPanelh ));
		
		Component box1 = Box.createHorizontalStrut(10);
		tipLabel = new JLabel( bean.getTip()  );
		this.add(descPanel);
		this.add(box1);
		this.add(tipLabel);
		this.setPreferredSize( new Dimension(0, descPanelh + 5  ) );
		
		String btnStr = bean.getBtn();
		if( !StringUtils.isEmpty( btnStr) ) {
			JButton jButton = new JButton(  btnStr );
			Component box2 = Box.createHorizontalStrut(10);
			this.add(box2);
			this.add(jButton);
			jButton.setEnabled(false);
			operBtn = jButton;
			operBtn.addActionListener(this);
		}else	if( bean.getRepeat()  ) {
			JButton jButton = new JButton(  "重试" );
			Component box2 = Box.createHorizontalStrut(10);
			this.add(box2);
			this.add(jButton);
			jButton.setEnabled(false);
			operBtn = jButton;
			operBtn.addActionListener(this);
		}
		
	}
	
	private void addClickListener(JLabel imageLabel2) {
		imageLabel2.addMouseListener( new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				try {
					String imgPath = bean.getParent().getPicDir() + "/" + (1 + index) + Prop.JPG;
					Desktop.getDesktop().open(new File(imgPath));
				} catch (IOException e1) {
					log.error("", e);
				}
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				imageLabel2.setCursor( new Cursor(Cursor.HAND_CURSOR) );
			}
			
			
		});
		
	}


	JLabel resLabel = new JLabel( );
	public void addResult(boolean res){
		try {
			ImageIcon imageIcon = null;
			if( res  ){
				 imageIcon = new ImageIcon(  ImgFactory.loadIconScale("yes.png" , 20 ,20 ) );
			}else{
				 imageIcon = new ImageIcon(  ImgFactory.loadIconScale("no.png" , 20 ,20 ) );
			}
			resLabel.setIcon(imageIcon);
			resLabel.setPreferredSize(new Dimension(20, 20));
			UIUtil.setCompBack(resLabel);
			
			
			if(  operBtn!=null ) {
				this.removeAll();
				this.add(descPanel);
				Component box1 = Box.createHorizontalStrut(10);
				Component box2 = Box.createHorizontalStrut(10);
				this.add(box1);
				this.add(resLabel);
				this.add(box2);
				this.add(operBtn);
			}else {
				this.remove(loadingPanel);
				this.add(resLabel);
			}
			
			this.revalidate();
			this.repaint();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JPanel loadingPanel = new JPanel( new BorderLayout());
	public void addLoading(){
		try {
			UIUtil.setCompBack(loadingPanel);
			loadingPanel.add(Box.createVerticalStrut(3) ,BorderLayout.NORTH);
//			URL resourceURL = NMSImgFactory.getResourceURL("loading1.gif");
			File file = new File("conf/loading1.gif");
			Gif gg = new Gif( file , 100);
			gg.setPreferredSize(new Dimension(20,20));
			loadingPanel.add(gg);
			
			if(  operBtn!=null ) {
				this.removeAll();
				this.add(descPanel);
				Component box1 = Box.createHorizontalStrut(10);
				Component box2 = Box.createHorizontalStrut(10);
				this.add(box1);
				this.add(loadingPanel);
				this.add(box2);
				this.add(operBtn);
			}else {
				this.remove(resLabel);
				this.remove(tipLabel);
				this.add(loadingPanel);
			}
			
			this.revalidate();
			this.repaint();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void showPic() {
		if(imageLabel == null) {
			return ;
		}
		
		int descPanelh = bean.getHh() + 7 ;
		descPanelh = descPanelh + imagehh;
		descPanel.add(imageLabel , BorderLayout.SOUTH );
		descPanel.setPreferredSize(new Dimension( 490 , descPanelh ));
		this.setPreferredSize( new Dimension(0, descPanelh + 6  ) );
		
	}
	
	public void hidePic(){
		if(imageLabel == null) {
			return ;
		}
		int descPanelh = bean.getHh() + 7 ;
		descPanel.remove(imageLabel   );
		descPanel.setPreferredSize(new Dimension( 490 , descPanelh ));
		this.setPreferredSize( new Dimension(0, descPanelh + 5  ) );
		
	}
	
	public void updateRes(boolean res) {
		addResult(res);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		StepThread curretThread = MainFrame.getInstance().getCurretThread();
		curretThread.goDoRun();
		operBtn.setEnabled(false);
	}

	public void setBtnEnable() {
		operBtn.setEnabled(true);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
	
	
}
